package com.baizhi.esdao.impl;

import com.baizhi.dao.EmpDao;
import com.baizhi.entity.Emp;
import com.baizhi.esdao.EmpEsDao;
import com.baizhi.utils.DateUtils;
import com.baizhi.utils.EsUtils;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.*;
@Repository
public class EmpEsDaoImpl implements EmpEsDao {
    @Override
    public void insert(Emp emp) {
        TransportClient client = EsUtils.getClient();
        Map<String,Object> map=new HashMap<>();
        map.put("name",emp.getName());
        map.put("bir",emp.getBir());
        map.put("age",emp.getAge());
        map.put("address",emp.getAddress());
        map.put("content",emp.getContent());
        client.prepareIndex("ems", "emp", emp.getId()+"").setSource(map).get();
        client.close();
    }

    @Override
    public void delete(int id) {

    }

    @Override
    public void update(Emp emp) {

    }

    @Override
    public List<Emp> selectByCon(String con) {
        TransportClient client = EsUtils.getClient();
        SearchResponse searchResponse = client.prepareSearch("ems").setTypes("emp")
                .setQuery(QueryBuilders.multiMatchQuery(con, "name", "content"))
                .highlighter(new HighlightBuilder().field("name").field("content").preTags("<span style='color:red'>").postTags("</span>"))
                .get();
        System.out.println(searchResponse);
        SearchHits hits = searchResponse.getHits();
        long totalHits = hits.getTotalHits();
        List<Emp> empList=new ArrayList<>();
        for(SearchHit hit:hits){
            String idStr = hit.getId();
            int id=Integer.parseInt(idStr);
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            String name =(String) sourceAsMap.get("name");
            String birStr=(String)sourceAsMap.get("bir");
            Date bir = DateUtils.strToDate(birStr);
            String content=(String)sourceAsMap.get("content");
            String address=(String) sourceAsMap.get("address");
            Integer age=(Integer)sourceAsMap.get("age");
            Emp emp=new Emp(id,name,bir,age,address,content);
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            HighlightField highlightField = highlightFields.get("name");
            if(highlightField!=null) {
                String nameHighLight = highlightField.fragments()[0].toString();
                emp.setName(nameHighLight);
            }
            HighlightField highlightField2 = highlightFields.get("content");
            if(highlightField2!=null) {
                String contentHighLight = highlightField2.fragments()[0].toString();
                emp.setContent(contentHighLight);
            }
            empList.add(emp);
        }
        System.out.println(empList);
        client.close();
        return empList;
    }
}
